import os
import numpy as np
from common.DATA import Data



class DataProcessor(Data):
    def __init__(self):
        super().__init__()
        self.output_path = self.create_next_folder()
        self.steps = []
        self.all_median = 1
        self.median = 0
        self.model_id = str(self.median)
        self.pre_y_row = None
        self.pre_y_col = None
        self.pre_y = None

    @staticmethod
    def create_next_folder():
        if not os.path.exists("./Output"):
            os.makedirs("./Output")
        folder_names = os.listdir("./Output")
        indices = [int(name) for name in folder_names if name.isdigit()]
        next_folder_index = np.max(indices) + 1 if indices else 1
        next_folder_name = "{:04d}".format(next_folder_index)
        next_folder_path = os.path.join("./Output", next_folder_name)
        os.makedirs(os.path.join(next_folder_path, "Img"))
        os.makedirs(os.path.join(next_folder_path, "Model_C"))
        os.makedirs(os.path.join(next_folder_path, "Model_E"))
        os.makedirs(os.path.join(next_folder_path, "Results"))
        return next_folder_path

    def add_step(self, step):
        self.steps.append(step)

    def process(self):
        for i in range(self.all_median):
            print("-------------------计算第{}个中间值-------------------".format(self.median))
            for step in self.steps:
                step.execute(self)
            self.median += 1
        print("-------------------计算完成-------------------")
        np.save(os.path.join(self.output_path, "Results", "pre_y.npy"), self.pre_y)


class JRMMDataProcessor(DataProcessor):
    def __init__(self):
        super().__init__()
        self.model_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]
        # self.model_array = [3, 4, 5, 6, 7, 8, 18, 19, 20, 21, 22, 23]
        self.model_index = 0

        self.all_median = 4
        self.median = 0

        self.model_id = str(self.median) + "_" + str(self.model_array[self.model_index])

        self.test_z = None
        self.test_y32 = None
        self.diff = None

        self.low_bit = 5
        self.division_trace = True

    def process(self):
        for i in range(self.median, self.all_median):
            for j in range(self.model_index, len(self.model_array)):
                self.model_id = str(self.median) + "_" + str(self.model_array[self.model_index])
                print("-------------------计算第{}个中间值-------------------".format(self.model_id))
                for step in self.steps:
                    step.execute(self)
                self.model_index = self.model_index + 1
                if self.model_index >= len(self.model_array):
                    self.model_index = 0
            self.median += 1

        print("-------------------计算完成-------------------")
        np.save(os.path.join(self.output_path, "Results", "pre_y.npy"), self.pre_y)
        y = np.load(r"C:\Users\zhanghaojin\Desktop\paper_code\dataset\jrmm\y_value_1.npy")
        np.save(os.path.join(self.output_path, "Results", "real_y.npy"), y[int(y.shape[0]*0.8):])
        z = np.load(r"C:\Users\zhanghaojin\Desktop\paper_code\dataset\jrmm\z_value_1.npy")
        np.save(os.path.join(self.output_path, "Results", "real_z.npy"), z[int(z.shape[0] * 0.8):])


